# 倒序输出字符串
# def reverse_1(s):
#     return s[::-1]   # 切片
# s = "hogwarts"
# print(reverse_1(s))

# def reverse_1(s):
#     s1 = ""
#     for i in s:
#         s1 = i + s1  # 拼接
#     return s1
# s = "hogwarts"
# print(reverse_1(s))

# def reverse_1(s):
#     s1 = list(s)  # 字符串到列表
#     print(s1)
#     s1.reverse()  # 逆序列表
#     print(s1)
#     s1 = "".join(s1)  # 连接字符串
#     print(s1)
# s = "hogwarts"
# reverse_1(s)


# 冒泡排序
# def bubble_sort(arr):   # 接受数组
#     num = len(arr)
#     for i in range(num-1):
#         for j in range(num-1):  # for j in range(num-i-1)
#             if arr[j] > arr[j+1]:
#                 arr[j], arr[j+1] = arr[j+1], arr[j]
#         print(arr)
#     return arr
# arr =[4, 9, 3, 5,2]
# print(bubble_sort(arr))

# def bubble_sort(arr):   # 接受数组
#     num = len(arr)
#     for i in range(num-1):
#         swap = False
#         for j in range(num-i-1):  # for j in range(num-1)
#             if arr[j] > arr[j+1]:
#                 arr[j], arr[j+1] = arr[j+1], arr[j]
#                 swap = True
#         if not swap:  # if (True)进判断   False 不进
#             break
#         print(arr)
#     return arr
# arr =[4, 9, 3, 5,2]
# bubble_sort(arr)

"""
arr=[4,7,5]
i=0,swap=False,len(arr)-1-i=3-1-0=2, --j:[0,2)=0,1:
j=0: 4 7 不满足if条件， j=1:7和5满足if条件
[4,7,5],swap=True
not swap=False,不执行break,继续
i=1,swap=False,len(arr)-1-i=3-1-1=1, --j:[0,1)=0:
j=0: 4 5 不满足if条件
[4,7,5],swap=False
not swap=True,执行break,跳出循环
"""

# def bubble_sort(arr, key=None, reverse=False):   # 接受数组
#     num = len(arr)
#     for i in range(num-1):
#         print(f"第{i+1}轮数组状态：{arr}")  # 每次开始时打印数组状态
#         swap = False
#         for j in range(num-i-1):  # for j in range(num-i-1)
#             if key:
#                 if key(arr[j]) > key(arr[j+1]):
#                     arr[j], arr[j+1] = arr[j+1], arr[j]
#                     print(f"交换后数组状态为{j}:{arr}")
#                     swap = True
#             else:
#                 if arr[j] > arr[j+1]:
#                      arr[j], arr[j+1] = arr[j+1], arr[j]
#                      print(f"交换后{j}:{arr}")
#                      swap = True
#         if not swap:  # if True进判断   False
#             break
#     if reverse:
#         print(arr[::-1])  # 最后排序的数组
#     return arr
# arr =[4, 9, 3, 5, 2]
# print(bubble_sort(arr, reverse=True))

# 选择排序
# def choice_sort(arr):
#     for i in range(len(arr)-1):   # 0,1,2,3,
#         small = i
#         for j in range(i+1, len(arr)):  # 位置 i=0-j=[1,5), i=1-[2,5) i=2-[3,5) i=3-[4,5)
#             if arr[j] < arr[small]:
#                 small = j
#         arr[i], arr[small] = arr[small], arr[i]
#         print(arr)
#     return arr
# arr = [4, 8, 3, 9, 2]  # 交换结果4-2 8-3 8-4 9-8  i= 0,1,2,3
# print(choice_sort(arr))

# 二分查找
# def num_find(arr, target):
#     low = 0
#     high = len(arr) - 1
#     while low <= high:
#             mid = (low + high) // 2
#             if arr[mid] == target:
#                 return mid  # 返回目标元素的索引
#             elif arr[mid] < target:
#                 low = mid + 1
#             else:
#                 high = mid - 1
#     return -1  # 目标元素不存在，则返回-1
# arr = [4,8,3,6,9]
# print(num_find(arr, 6))














